using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Cashier;
using JlPay.Sdk.Models.Cashier;

namespace JlPay.Sdk.Examples.Cashier.OrderQuery
{
    /// <summary>
    /// 订单查询独立示例程序
    /// </summary>
    public class OrderQueryDemo
    {
        /// <summary>
        /// 主程序入口
        /// </summary>
        /// <param name="args">命令行参数</param>
        /// <returns>任务</returns>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("==========================================");
            Console.WriteLine("===== 订单查询独立示例程序 =====");
            Console.WriteLine("==========================================");
            Console.WriteLine();

            await OrderQueryDemoAsync();

            Console.WriteLine("==========================================");
            Console.WriteLine("===== 示例运行完毕 =====");
            Console.WriteLine("==========================================");
        }

        /// <summary>
        /// 创建配置对象
        /// </summary>
        /// <returns>配置对象</returns>
        private static Config CreateConfig()
        {
            return DemoConfig.CreateConfigWithCrypto();
        }

        /// <summary>
        /// 构建订单查询请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static OrderQueryRequest BuildOrderQueryRequest()
        {
            var request = new OrderQueryRequest
            {
                MerchNo = "849584358120018",
                OutTradeNo = "ld201911023465529"
            };

            return request;
        }

        /// <summary>
        /// 订单查询示例
        /// </summary>
        public static async Task OrderQueryDemoAsync()
        {
            Console.WriteLine("开始执行订单查询示例");
            
            try
            {
                // 创建配置对象和收银台服务
                var config = CreateConfig();
                var cashierService = new CashierService(config);
                
                // 创建订单查询请求
                var request = BuildOrderQueryRequest();
                
                // 发送请求
                var response = await cashierService.QueryOrderAsync(request);
                
                // 处理响应结果
                response.HandleResponse("订单查询", config);
            }
            catch (Exception ex)
            {
                var config = CreateConfig();
                var response = new OrderQueryResponse();
                response.HandleResponseWithException("订单查询", config, ex);
            }
            
            Console.WriteLine("订单查询示例执行完毕");
        }
    }
} 